% figure for the number of obs

%% Number of obs
% number of obs over time 
n_year = [];
for i=1:1:length(year_val)
    n_year = [n_year; sum(year==year_val(i))];
end

figure
plot(unique(year), n_year,'*-','linewidth', 3,'Markersize',15)
title('number of obs by TIME');
set(gca, 'fontsize', 15, 'linewidth', 3);

% number of obs by combofips
n_combofips = [];
for i=1:1:length(xtval)
    n_combofips = [n_combofips; sum(xtflag == xtval(i))];
end

figure
plot(sort(n_combofips),'*-','linewidth', 3,'Markersize',15)
title('number of obs by Combofips (sorted)');
set(gca, 'fontsize', 15, 'linewidth', 3);

% number of obs by time (with quantiles)
n_yearfips = [];
for i=1:1:length(xtval2)

    temp_xtflag = xtflag(xtflag2==xtval2(i));
    temp_xtval = unique(temp_xtflag);
    temp_n = [];
    for j=1:1:length(temp_xtval)
        temp_n = [temp_n; sum(temp_xtflag==temp_xtval(j))];
    end
    
    temp_line = [min(temp_n), quantile(temp_n, [0.1, 0.25, 0.5, 0.75, 0.9]), max(temp_n)];
    
%     temp_line = [quantile(temp_n, [0.1, 0.25, 0.5, 0.75, 0.9])];
    
    n_yearfips = [n_yearfips; temp_line];
end

figure
plot(n_yearfips, '*-', 'linewidth', 3);
title('number of obs by Combofips (by quantiles [0.1, 0.25, 0.5, 0.75, 0.9])');
set(gca, 'fontsize', 15, 'linewidth', 3);

%% Average values
% average values of obs over time 
n_val = [];
for i=1:1:length(year_val)
    n_val = [n_val; mean(YY(year==year_val(i),:))];
end

figure
plot(unique(year), n_val,'*-','linewidth', 3,'Markersize',15)
title('average value of obs by TIME');
set(gca, 'fontsize', 15, 'linewidth', 3);

% average values of obs by combofips
n_val2 = [];
for i=1:1:length(xtval)
    n_val2 = [n_val2; mean(YY(xtflag == xtval(i),1))];
end

figure
plot(sort(n_val2),'*-','linewidth', 3,'Markersize',15)
title('average value of obs by Combofips (sorted)');
set(gca, 'fontsize', 15, 'linewidth', 3);

